package com.qwert.shortlink.admin.controller;

import com.qwert.shortlink.admin.common.convention.result.Result;
import com.qwert.shortlink.admin.dto.req.user.UserLoginReqDTO;
import com.qwert.shortlink.admin.dto.req.user.UserRegisterReqDTO;
import com.qwert.shortlink.admin.dto.req.user.UserUpdateReqDTO;
import com.qwert.shortlink.admin.dto.resp.user.UserLoginRespDTO;
import com.qwert.shortlink.admin.dto.resp.user.UserRespDTO;
import com.qwert.shortlink.admin.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author qwert
 * @version 1.0.0
 * @create 2024/6/25 9:37
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/shortlink/admin")
public class UserController {

//    @Autowired
//    private IUserService userService;
    private final IUserService userService;

    /**
     * 根据用户名查找用户
     */
    @GetMapping("/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable String username) {
        return Result.success(userService.getUserByUsername(username));
    }

    /**
     * 查询用户名是否存在
     * @param username 用户名
     * @return 用户是否存在结果
     */
    @GetMapping("/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username) {
        return Result.success(userService.hasUsername(username));
    }

    /**
     * 根据用户数据注册新账户
     * @param userRegisterReqDTO 用户注册请求数据
     * @return  注册行数
     */
    @PostMapping("/v1/user")
    public Result<Integer> register(@RequestBody UserRegisterReqDTO userRegisterReqDTO) {
        return Result.success(userService.register(userRegisterReqDTO));
    }

    /**
     *  更新用户
     */
    @PutMapping("/v1/user")
    public Result<Integer> update(@RequestBody UserUpdateReqDTO userUpdateReqDTO) {
        return Result.success(userService.update(userUpdateReqDTO));
    }

    /**
     *  用户登录
     */
    @PostMapping("/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO userLoginReqDTO) {
        return Result.success(userService.login(userLoginReqDTO));
    }

    /**
     *  检查用户登录
     */
    @GetMapping("/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token){
        return Result.success(userService.checkLogin(username, token));
    }

    /**
     *  用户退出登录
     */
    @DeleteMapping("/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token) {
        userService.logout(username, token);
        return Result.success();
    }

}
